// pages/word_game/word-practice.js
const utils = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    action: {method: ''},
    wordList:[],
    currentWord: null,
    index: -1,
    round: 1,
    answer: ''
  },
  audioPlay: function(e) {
    this.setData({
      action: {
        method: 'play'
      }
    });
  },
  checkWord: function(e) {
    if(this.data.round <= 1) {
      console.log('检测',this.data.currentWord);
      if(this.data.answer === '') {
        wx.showToast({
          title: '答案不能为空',
          icon: 'none'
        });
        return ;
      }
      // 英译汉
      let isHas = this.data.currentWord.desc.indexOf(this.data.answer);
      console.log('答案是否正确',isHas);
      if(isHas != -1) {
        let currentIndex = this.data.index++;
        this.setData({index: currentIndex,currentWord: this.data.wordList[currentIndex]});
        this.nextWord(currentIndex);
      } else {
        wx.showToast({
          title: '答案错误',
          icon: 'none'
        });
      }
      console.log('英译汉',this.data.answer);
    } else {
      // 汉译英
      let isHas = this.data.currentWord.name.indexOf(this.data.answer);
      if(this.data.currentWord.name === this.data.answer) {
        let currentIndex = this.data.index++;
        this.nextWord(currentIndex);
      } else {
        wx.showToast({
          title: '答案错误',
          icon: 'none'
        });
      }
      console.log('汉译英',e.detail.value);
    }
  },
  inputWord: function(e) {
    console.log('sss',this.data.currentWord);
    this.setData({answer: e.detail.value});
    if(this.data.answer.length >= this.data.currentWord.name.length){
      if(this.data.answer === this.data.currentWord.name) {
        wx.showToast({
          title: '回答正确',
          icon: 'none'
        });
      } else {
        wx.showToast({
          title: '回答错误',
          icon: 'none'
        })
      }
    }
  },
  showAnswer: function() {
    if(this.data.currentWord === null || this.data.currentWord === undefined) {
      wx.showToast({
        title: '已经最后一个了',
        icon: 'none'
      });
    }
    let showAnswer = '';
    if(this.data.round <= 1) {
      showAnswer = this.data.currentWord.desc;
    } else {
      showAnswer = this.data.currentWord.name;
    }
    this.setData({
      answer: showAnswer
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var wordType = wx.getStorageSync(utils.wordType);
    var that = this;
    wx.showLoading({
      title: '正在加载数据...',
    });
    setTimeout(() => {
      wx.request({
        method: 'POST',
        url: 'http://rw.ylapi.cn/reciteword/wordlist.u?uid=11422&appkey=055aa48e889bca3fe7f6a27702af80d8&' + 'class_id=' + wordType.class_id + '&course='+ wordType.course_num,
        // header: {
        //     'Content-Type': 'application/json'
        // },
        success: function(res) {
          console.log('ttt',res);
          if(res.data.code == 1000){
            console.log('ssss',res);
              that.setData({wordList: res.data.datas, currentWord: res.data.datas[0],index: 0});
              wx.setStorageSync(utils.remoteWordList, res.data.datas);
              that.nextWord(0);
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
          wx.hideLoading({
            success: (res) => {},
          })
        },
        fail: function() {
          wx.showToast({
              title: '请求错误',
              icon: 'none',
              duration: 1500
          })
            console.log("error");
        },
      });
    },500);
  },

  nextWord: function(i) {
    if(i >= -1) {
      let idx = i + 1;
      let curtWord = this.data.wordList;
      let round = Math.round(Math.random() * 3);
      console.log('随机数',round);
      this.setData({index:idx,currentWord: curtWord[idx],round: round,answer: ''});
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})